<?php

// +----------------------------------------------------------------------
// | qjf [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | explain: 用户权限验证服务
// +----------------------------------------------------------------------

namespace app\common\service;

/**
 *  用户权限验证服务
 *  验证用户是否有权限访问某个控制器的某个方法
 * Class UserAuthService
 * @package app\common\service
 */
class UserAuthService
{

    /**
     * 当前控制器
     * @var null
     */
    protected $currentController;

    /**
     * 当前方法
     * @var null
     */
    protected $currentAction;

    /**
     * 默认配置
     * @var array
     */
    protected $config = [
        'auth_on' => true,              // 权限开关
    ];


    /***
     * 构造方法
     * UserAuthService constructor.
     */
    public function __construct($controller, $action, $module = 'uapi')
    {
        $controller = $this->parseNodeStr($controller);
        $controller = CommonService::lineToHump($controller);
        $controller = ucfirst($controller);
        // 判断是否有子目录
        if (strpos($controller, '.')) {
            // 有子目录，转换成命名空间
            $controller = explode('.', $controller);
            $controller = array_map('ucfirst', $controller);
            $controller = implode('\\', $controller);
        }
        $controller = "app\\{$module}\\controller\\{$controller}";
        $this->currentController = $controller;
        $this->currentAction = $action;
        return $this;
    }


    /**
     * 检查未登录情况下是否有权限访问某个控制器的某个方法
     * @param $node
     * @return bool
     */
    public function checkAction()
    {
        // 判断是否开启权限验证
//        if (!$this->config['auth_on']) {
//            // return true;
//        }
        try {
            $class = new \ReflectionClass($this->currentController);
            $arrNeedLoginAction = $class->getStaticPropertyValue('needLoginAction');
            //登录后返回到当前
            $arrLoginBack = $class->getStaticPropertyValue('loginBack',[]);
            if(in_array($this->currentAction, $arrLoginBack)){
                session('back_redirect_Url', request()->url());
            }

            //需要登录的方法
            if (in_array('*', $arrNeedLoginAction) || in_array($this->currentAction, $arrNeedLoginAction)) {
                return false;
            }
            return true;
        } catch (\ReflectionException $e) {
            //抛出异常
            throw new \RuntimeException($this->currentController . '控制器不存在');
        }
    }


    /**
     * 驼峰转下划线规则
     * @param string $node
     * @return string
     */
    public function parseNodeStr($node)
    {
        $array = explode('/', $node);
        foreach ($array as $key => $val) {
            if ($key == 0) {
                $val = explode('.', $val);
                foreach ($val as &$vo) {
                    $vo = CommonService::humpToLine(lcfirst($vo));
                }
                $val = implode('.', $val);
                $array[$key] = $val;
            }
        }
        $node = implode('/', $array);
        return $node;
    }

}